﻿using AutoMapper;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform.HdlScadaAlarmRealTime;
using HuiLian.Service.HdlScada.AlarmRealTime.Output;
using HuiLian.Service.HdlScada.AlarmRealTime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HuiLian.Repository.Platform.HdlScadaAlarmHistory;
using HuiLian.Service.HdlScada.HdlScadaAlarmHistory.Output;
using HuiLian.Common.Extensions;

namespace HuiLian.Service.HdlScada.HdlScadaAlarmHistory
{
    public class HdlScadaAlarmHistoryService : IHdlScadaAlarmHistoryService
    {
        private readonly IMapper _mapper;
        private readonly IHdlScadaAlarmHistoryRepository _hdlScadaAlarmHistoryRepository;

        public HdlScadaAlarmHistoryService(IMapper mapper, IHdlScadaAlarmHistoryRepository hdlScadaAlarmHistoryRepository)
        {
            _mapper = mapper;
            _hdlScadaAlarmHistoryRepository = hdlScadaAlarmHistoryRepository;
        }

        /// <summary>
        /// 获取实时报警记录
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _hdlScadaAlarmHistoryRepository.Select.Where(p => p._id == id.ToString()).IncludeMany(p => p.Anls).IncludeMany(p => p.Acrs).FirstAsync();
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询调用时间表信息
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<HdlScadaAlarmHistoryEntity> input)
        {
            //var key = input.Filter?.Label;

            if (input.DynamicFilter != null && input.DynamicFilter.Filters != null && input.DynamicFilter.Filters.Count > 0)
            {
                foreach (var item in input.DynamicFilter.Filters)
                {
                    if(item.Field == "stime")
                    {
                        string[] value = item.Value.ToString().Split(",");
                        if (value.Length != 2) return ResponseOutput.NotOk("时间范围格式不正确");
                        DateTime beginDt = Convert.ToDateTime(value[0]);
                        DateTime endDt = Convert.ToDateTime(value[1]);
                        endDt = endDt.AddDays(1);
                        item.Value = $"{beginDt.ToTimestamp()},{endDt.ToTimestamp()}";
                    }
                }
            }

            var list = await _hdlScadaAlarmHistoryRepository.Select
            .WhereDynamicFilter(input.DynamicFilter)
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
            .Count(out var total)
            .OrderByDescending(true, c => c.stime)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync(p => new HdlScadaAlarmHistoryListOutput { stime = p.stime });

            var data = new PageOutput<HdlScadaAlarmHistoryListOutput>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }
    }
}
